﻿using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Xml;

namespace EIS.ServerAdapter
{
    public class RestAdapter
    {
        public static DataSet HttpGet(string url)
        {
            try
            {
                var req = WebRequest.Create(url) as HttpWebRequest;

                Debug.Assert(req != null, "req != null");
                req.Method = "GET";
                req.Accept = "*/*";

                var resp = req.GetResponse() as HttpWebResponse;

                var reader = new StreamReader(resp.GetResponseStream());
                var result = new StringReader(reader.ReadToEnd());
                var ds = new DataSet();
                ds.ReadXml(result);
/*                if (ds.Tables.Count == 0)
                {
                    return null;
                }
                else
                {*/
                    return ds;
                //}
                

            }
            catch (WebException wex)
            {
                throw wex;
            }
        }

        public static DataSet HttpPost(string url, string xml)
        {

            var ds = new DataSet();

            //Declare an HTTP-specific implementation of the WebRequest class.
            HttpWebRequest objHttpWebRequest;

            //Declare an HTTP-specific implementation of the WebResponse class
            HttpWebResponse objHttpWebResponse = null;

            //Declare a generic view of a sequence of bytes
            Stream objRequestStream = null;
            Stream objResponseStream = null;

            //Declare XMLReader
            XmlTextReader objXMLReader;

            //Creates an HttpWebRequest for the specified URL.
            objHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);



            try
            {
                //---------- Start HttpRequest

                //Set HttpWebRequest properties
                byte[] bytes;
                bytes = System.Text.Encoding.UTF8.GetBytes(xml)
                ;
                objHttpWebRequest.Method = "POST";
                objHttpWebRequest.ContentLength = bytes.Length;
                objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
                objHttpWebRequest.Accept = "*/*";
                //Get Stream object
                objRequestStream = objHttpWebRequest.GetRequestStream();

                //Writes a sequence of bytes to the current stream
                objRequestStream.Write(bytes, 0, bytes.Length);

                //Close stream
                objRequestStream.Close();

                //---------- End HttpRequest

                //Sends the HttpWebRequest, and waits for a response.
                objHttpWebResponse = (HttpWebResponse) objHttpWebRequest.GetResponse();

                //---------- Start HttpResponse
                if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    //Get response stream
                    objResponseStream = objHttpWebResponse.GetResponseStream();

                    //Load response stream into XMLReader
                    objXMLReader = new XmlTextReader(objResponseStream);

                    //Declare Output DataSet


                    ds.ReadXml(objXMLReader);

                    //Close XMLReader
                    objXMLReader.Close();
                }

                //Close HttpWebResponse
                objHttpWebResponse.Close();
            }
            catch (WebException we)
            {
                //TODO: Add custom exception handling
                throw ;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                //Close connections
                if (objRequestStream != null) objRequestStream.Close();
                if (objResponseStream != null) objResponseStream.Close();
                if (objHttpWebResponse != null) objHttpWebResponse.Close();

                //Release objects
                objXMLReader = null;
                objRequestStream = null;
                objResponseStream = null;
                objHttpWebResponse = null;
                objHttpWebRequest = null;
            }

            //Return
            
            return ds;
        }

    }
}
